//
//  FreezeFormView.m
//  etionUI
//
//  Created by wangjian on 12/24/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "FreezeFormView.h"

#define SEP_VIEW_W 5

@implementation CGradientView
{
    CGGradientRef _gradient;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.opaque = NO;
        self.userInteractionEnabled = NO;
    }
    return self;
}

- (void)setGradientDir:(EGradientDirection)gradientDir
{
    _gradientDir = gradientDir;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (self.gradientDir == EGradientDirectionLeft)
    {
        CGContextDrawLinearGradient(context, self.gradient, CGPointMake(0, 0), CGPointMake(self.width, 0), kCGGradientDrawsAfterEndLocation);
    }
    else if (self.gradientDir == EGradientDirectionDown)
    {
        CGContextDrawLinearGradient(context, self.gradient, CGPointMake(0, 0), CGPointMake(0, self.height), kCGGradientDrawsAfterEndLocation);
    }
    else if (self.gradientDir == EGradientDirectionUp)
    {
        CGContextDrawLinearGradient(context, self.gradient, CGPointMake(0, self.height), CGPointMake(0, 0), kCGGradientDrawsAfterEndLocation);
    }
}

-(CGGradientRef)gradient
{
	if(_gradient == NULL)
	{
		CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat colors[] =
        {
            192.0 / 255.0, 192.0 / 255.0, 192.0 / 255.0, 0.70,
            1.0, 1.0, 1.0, 0.00
        };
        _gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
		CGColorSpaceRelease(rgb);
	}
	return _gradient;
}

@end

@interface CFreezeFormView()

@property (nonatomic, retain) CFormView *leftTopFormView;
@property (nonatomic, retain) CFormView *topMiddleFormView;
@property (nonatomic, retain) CFormView *leftMiddleFormView;
@property (nonatomic, retain) CFormView *middleFormView;
@property (nonatomic, retain) CFormView *leftBottomFormView;
@property (nonatomic, retain) CFormView *bottomMiddleFormView;

@property (nonatomic, assign) CGSize curContentDisplaySize;

@end

@implementation CFreezeFormView
{
    CGPoint leftTopSepPoint;
    CGPoint leftBottomSepPoint;
    
    CGradientView *_leftMiddleSepView;
    CGradientView *_topMiddleSepView;
    CGradientView *_bottomMiddleSepView;
    
    UIView *_maskView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.opaque = NO;
        self.style = EFreezeFormStyleClassic;
        self.borderSize = 0;
        self.borderColor = [UIColor lightGrayColor];
        
        self.renderColor = [UIColor colorWithHex:0x99cf6d];
        
        _maskView = [[[UIView alloc] initWithFrame:CGRectInset(self.bounds, 0, self.borderSize)] autorelease];
        _maskView.layer.masksToBounds = YES;
        [self addSubview:_maskView];
        
        _contentView = [[[UIView alloc] initWithFrame:_maskView.bounds] autorelease];
        [_maskView addSubview:_contentView];
        
//        self.contentSize = self.bounds.size;
        self.borderCornerRadius = 0;
        self.curContentDisplaySize = _contentView.bounds.size;
    }
    return self;
}

- (void)dealloc
{
    [_curSelectedIndex release];
    [_borderColor release];
    
    [_leftTopFormView release];
    [_leftBottomFormView release];
    [_leftMiddleFormView release];
    [_middleFormView release];
    [_topMiddleFormView release];
    [_bottomMiddleFormView release];
    
    [super dealloc];
}

- (CGRect)contentFrame
{
    return _maskView.frame;
}

- (void)drawRect:(CGRect)rect
{
    if (self.borderSize > 0)
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGFloat lineWidth = self.borderSize;
        CGFloat cornerRadius = self.borderCornerRadius;
        CGFloat offSet = 0;
        CGFloat scale = [UIScreen mainScreen].scale;
        
        lineWidth /= scale;
        
        if(((NSUInteger)self.borderSize & 1) == 1)
        {
            offSet = lineWidth / scale;
        }
        
        rect = CGRectMake(lineWidth - offSet, lineWidth - offSet, self.curContentDisplaySize.width + lineWidth * scale, self.curContentDisplaySize.height + lineWidth * scale);
        CGContextMoveToPoint(context, CGRectGetMinX(rect) + cornerRadius, CGRectGetMinY(rect));
        CGContextAddArc(context, CGRectGetMaxX(rect) - cornerRadius, CGRectGetMinY(rect) + cornerRadius, cornerRadius, 3 * M_PI / 2, 0, 0);
        CGContextAddArc(context, CGRectGetMaxX(rect) - cornerRadius, CGRectGetMaxY(rect) - cornerRadius, cornerRadius, 0, M_PI / 2, 0);
        CGContextAddArc(context, CGRectGetMinX(rect) + cornerRadius, CGRectGetMaxY(rect) - cornerRadius, cornerRadius, M_PI / 2, M_PI, 0);
        CGContextAddArc(context, CGRectGetMinX(rect) + cornerRadius, CGRectGetMinY(rect) + cornerRadius, cornerRadius, M_PI, 3 * M_PI / 2, 0);
        
        CGContextSetStrokeColorWithColor(context, self.borderColor.CGColor);
        CGContextSetLineWidth(context, lineWidth);
        CGContextStrokePath(context);
    }
}

- (void)setBorderSize:(CGFloat)borderSize
{
    _borderSize = borderSize;
    _maskView.frame = CGRectInset(self.bounds, self.borderSize, self.borderSize);
    _contentView.frame = _maskView.bounds;
}

- (void)setBorderCornerRadius:(CGFloat)borderCornerRadius
{
    _borderCornerRadius = borderCornerRadius;
    _maskView.layer.cornerRadius = borderCornerRadius;
}

- (void)setCurContentDisplaySize:(CGSize)curContentDisplaySize
{
    _curContentDisplaySize = curContentDisplaySize;
    _maskView.frame = CGRectMake(_maskView.left, _maskView.top, curContentDisplaySize.width, curContentDisplaySize.height);
    if (self.leftBottomFormView != nil)
    {
        self.leftBottomFormView.frame = CGRectMake(self.leftBottomFormView.left, curContentDisplaySize.height-self.leftBottomFormView.height, self.leftBottomFormView.width, self.leftBottomFormView.height);
    }
    if (self.bottomMiddleFormView != nil)
    {
        self.bottomMiddleFormView.frame = CGRectMake(self.bottomMiddleFormView.left, curContentDisplaySize.height-self.bottomMiddleFormView.height, self.bottomMiddleFormView.width, self.bottomMiddleFormView.height);
    }
    [self setNeedsDisplay];
}

- (CGFloat)heightForRow:(NSUInteger)row
{
    if (row < self.freezeTopRowNum)
    {
        return [self.topMiddleFormView heightForRow:row];
    }
    else if (row > self.rowNum-self.freezeBottomRowNum-1)
    {
        return [self.bottomMiddleFormView heightForRow:row-(self.rowNum-self.freezeBottomRowNum)];
    }
    else
    {
        return [self.middleFormView heightForRow:row-self.freezeTopRowNum];
    }
}

- (CGFloat)widthForCol:(NSUInteger)col
{
    if (col < self.freezeColNum)
    {
        return [self.leftMiddleFormView widthForCol:col];
    }
    else
    {
        return [self.middleFormView widthForCol:col-self.freezeColNum];
    }
}

- (void)setHeight:(CGFloat)height forRow:(NSUInteger)row animated:(BOOL)animated
{
    if (row < self.freezeTopRowNum)
    {
        [self.topMiddleFormView setHeight:height forRow:row animated:animated];
        [self.leftTopFormView setHeight:height forRow:row animated:animated];
    }
    else if (row > self.rowNum-self.freezeBottomRowNum-1)
    {
        [self.leftBottomFormView setHeight:height forRow:row-(self.rowNum-self.freezeBottomRowNum) animated:animated];
        [self.bottomMiddleFormView setHeight:height forRow:row-(self.rowNum-self.freezeBottomRowNum) animated:animated];
    }
    else
    {
        [self.leftMiddleFormView setHeight:height forRow:row-self.freezeTopRowNum animated:animated];
        [self.middleFormView setHeight:height forRow:row-self.freezeTopRowNum animated:animated];
    }
    [self adjustFormViewFrame];
}

- (void)setWidth:(CGFloat)width forCol:(NSUInteger)col animated:(BOOL)animated
{
    if (col < self.freezeColNum)
    {
        [self.leftMiddleFormView setWidth:width forCol:col animated:animated];
        [self.leftBottomFormView setWidth:width forCol:col animated:animated];
        [self.leftTopFormView setWidth:width forCol:col animated:animated];
        [self adjustFormViewFrame];
    }
    else
    {
        [self.topMiddleFormView setWidth:width forCol:col-self.freezeColNum animated:animated];
        [self.bottomMiddleFormView setWidth:width forCol:col-self.freezeColNum animated:animated];
        [self.middleFormView setWidth:width forCol:col-self.freezeColNum animated:animated];
    }
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    if (self.superview != nil)
    {
        [self reload];
    }
}

//内部index转换为外部index
- (CFormCellIndex *)realIndexWithIndex:(CFormCellIndex *)index inFormView:(CFormView *)view
{
    CFormCellIndex *realIndex = [CFormCellIndex indexWithtRow:index.row col:index.col];
    if (self.topMiddleFormView == view || self.middleFormView == view || self.bottomMiddleFormView == view)
    {
        realIndex.col += self.freezeColNum;
    }
    if (self.leftMiddleFormView == view || self.middleFormView == view)
    {
        realIndex.row += self.freezeTopRowNum;
    }
    if (self.leftBottomFormView == view || self.bottomMiddleFormView == view)
    {
        realIndex.row += self.rowNum-self.freezeBottomRowNum;
    }
    return realIndex;
}

//外部index转换为内部index
- (CFormCellIndex *)realIndexWithIndex:(CFormCellIndex *)index
{
    CFormCellIndex *realIndex = [CFormCellIndex indexWithtRow:index.row col:index.col];
    if (realIndex.row > self.rowNum-self.freezeBottomRowNum-1)
    {
        realIndex.row -= self.rowNum-self.freezeBottomRowNum;
    }
    else if (index.row >= self.freezeTopRowNum)
    {
        realIndex.row -= self.freezeTopRowNum;
    }
    if (index.col >= self.freezeColNum)
    {
        realIndex.col -= self.freezeColNum;
    }
    return realIndex;
}

- (CFormView *)formViewForIndex:(CFormCellIndex *)index
{
    if (index == nil)
    {
        return nil;
    }
    if (index.row < self.freezeTopRowNum && index.col < self.freezeColNum)
    {
        return self.leftTopFormView;
    }
    else if (index.row < self.freezeTopRowNum)
    {
        return self.topMiddleFormView;
    }
    else if (index.row > self.rowNum-self.freezeBottomRowNum-1 && index.col < self.freezeColNum)
    {
        return self.leftBottomFormView;
    }
    else if (index.row > self.rowNum-self.freezeBottomRowNum-1)
    {
        return self.bottomMiddleFormView;
    }
    else if (index.col < self.freezeColNum)
    {
        return self.leftMiddleFormView;
    }
    else
    {
        return self.middleFormView;
    }
}

- (void)reload
{
    [self.leftTopFormView removeFromSuperview];
    self.leftTopFormView = nil;
    [self.topMiddleFormView removeFromSuperview];
    self.topMiddleFormView = nil;
    [self.leftMiddleFormView removeFromSuperview];
    self.leftMiddleFormView = nil;
    [self.middleFormView removeFromSuperview];
    self.middleFormView = nil;
    [self.leftBottomFormView removeFromSuperview];
    self.leftBottomFormView = nil;
    [self.bottomMiddleFormView removeFromSuperview];
    self.bottomMiddleFormView = nil;
    [_leftMiddleSepView removeFromSuperview], _leftMiddleSepView = nil;
    [_topMiddleSepView removeFromSuperview], _topMiddleSepView = nil;
    [_bottomMiddleSepView removeFromSuperview], _bottomMiddleSepView = nil;

    self.middleFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
    self.middleFormView.delegate = self;
    if (self.freezeTopRowNum>0)
    {
        self.topMiddleFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
        self.topMiddleFormView.delegate = self;
    }
    if (self.freezeColNum>0)
    {
        self.leftMiddleFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
        self.leftMiddleFormView.delegate = self;
    }
    if (self.freezeBottomRowNum>0)
    {
        self.bottomMiddleFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
        self.bottomMiddleFormView.delegate = self;
    }
    if (self.freezeTopRowNum>0 && self.freezeColNum>0)
    {
        self.leftTopFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
        self.leftTopFormView.delegate = self;
    }
    if (self.freezeColNum>0 && self.freezeBottomRowNum>0)
    {
        self.leftBottomFormView = [[[CFormView alloc] initWithFrame:CGRectZero] autorelease];
        self.leftBottomFormView.delegate = self;
    }
    
    [self.contentView addSubview:self.middleFormView];
    [self.contentView addSubview:self.topMiddleFormView];
    [self.contentView addSubview:self.leftMiddleFormView];
    [self.contentView addSubview:self.bottomMiddleFormView];
    [self.contentView addSubview:self.leftTopFormView];
    [self.contentView addSubview:self.leftBottomFormView];
    
    if (self.freezeColNum > 0)
    {
        _leftMiddleSepView = [[[CGradientView alloc] initWithFrame:CGRectZero] autorelease];
        [self.contentView addSubview:_leftMiddleSepView];
        _leftMiddleSepView.gradientDir = EGradientDirectionLeft;
        _leftMiddleSepView.userInteractionEnabled = NO;
    }
    
    if (self.freezeTopRowNum > 0)
    {
        _topMiddleSepView = [[[CGradientView alloc] initWithFrame:CGRectZero] autorelease];
        [self.contentView addSubview:_topMiddleSepView];
        _topMiddleSepView.gradientDir = EGradientDirectionDown;
        _topMiddleSepView.userInteractionEnabled = NO;
    }
    
    if (self.freezeBottomRowNum > 0)
    {
        _bottomMiddleSepView = [[[CGradientView alloc] initWithFrame:CGRectZero] autorelease];
        [self.contentView addSubview:_bottomMiddleSepView];
        _bottomMiddleSepView.gradientDir = EGradientDirectionUp;
        _bottomMiddleSepView.userInteractionEnabled = NO;
    }
    
    [self adjustFormViewFrame];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self adjustFormViewFrame];
}

- (void)adjustFormViewFrame
{
    _maskView.frame = CGRectInset(self.bounds, self.borderSize, self.borderSize);
    self.contentView.frame = CGRectMake(0, 0, self.width-2*self.borderSize, self.height-2*self.borderSize);
    leftTopSepPoint = CGPointMake(0, 0);
    for (NSUInteger row=0; row<self.freezeTopRowNum; row++)
    {
        leftTopSepPoint.y += [self.topMiddleFormView heightForRow:row];
    }
    for (NSUInteger col=0; col<self.freezeColNum; col++)
    {
        leftTopSepPoint.x += [self.leftMiddleFormView widthForCol:col];
    }
    
    leftBottomSepPoint = CGPointMake(leftTopSepPoint.x, self.contentView.height);
    for (NSUInteger row=0; row<self.freezeBottomRowNum; row++)
    {
        leftBottomSepPoint.y -= [self.bottomMiddleFormView heightForRow:row];
    }
    
    CGSize middleViewSize = CGSizeMake(self.contentView.width-leftTopSepPoint.x, leftBottomSepPoint.y-leftTopSepPoint.y);
    
    self.middleFormView.frame = CGRectMake(leftTopSepPoint.x, leftTopSepPoint.y, middleViewSize.width, middleViewSize.height);
    self.topMiddleFormView.frame = CGRectMake(leftTopSepPoint.x, 0, middleViewSize.width, leftTopSepPoint.y);
    self.leftMiddleFormView.frame = CGRectMake(0, leftTopSepPoint.y, leftTopSepPoint.x, middleViewSize.height);
    self.bottomMiddleFormView.frame = CGRectMake(leftBottomSepPoint.x, leftBottomSepPoint.y, middleViewSize.width, self.contentView.height - leftBottomSepPoint.y);
    self.leftTopFormView.frame = CGRectMake(0, 0, leftTopSepPoint.x, leftTopSepPoint.y);
    self.leftBottomFormView.frame = CGRectMake(0, leftBottomSepPoint.y, leftBottomSepPoint.x, self.bottomMiddleFormView.height);
    
    _leftMiddleSepView.frame = CGRectMake(leftTopSepPoint.x, 0, SEP_VIEW_W, self.contentView.height);
    _leftMiddleSepView.alpha = self.middleFormView.contentOffset.x > 0 ? 1.0 : 0.0;
    
    _topMiddleSepView.frame = CGRectMake(0, leftTopSepPoint.y, self.contentView.width, SEP_VIEW_W);
    _topMiddleSepView.alpha = self.middleFormView.contentOffset.y > 0 ? 1.0 : 0.0;

    _bottomMiddleSepView.frame = CGRectMake(0, leftBottomSepPoint.y-SEP_VIEW_W, self.contentView.width, SEP_VIEW_W);
    _bottomMiddleSepView.alpha = self.middleFormView.contentSize.height > self.middleFormView.height ? 1.0 : 0.0;
    
    self.curContentDisplaySize = [self contentDisplaySize];
}

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndex:(CFormCellIndex *)index
{
    CFormView *formView = [self formViewForIndex:index];
    return [formView dequeueReusableCellWithIdentifier:identifier];
}

- (CFormCellView *)cellAtIndex:(CFormCellIndex *)index
{
    CFormView *formView = [self formViewForIndex:index];
    return [formView cellAtIndex:[self realIndexWithIndex:index]];
}

- (CGRect)notFreezeContentFrame
{
    return self.middleFormView.frame;
}

- (CFormCellIndex *)indexForCell:(CFormCellView *)cell
{
    CFormCellIndex *index = [self.middleFormView indexForCell:cell];
    if (index == nil)
    {
        index = [self.topMiddleFormView indexForCell:cell];
    }
    if (index == nil)
    {
        index = [self.leftMiddleFormView indexForCell:cell];
    }
    if (index == nil)
    {
        index = [self.leftTopFormView indexForCell:cell];
    }
    if (index == nil)
    {
        index = [self.leftBottomFormView indexForCell:cell];
    }
    if (index == nil)
    {
        index = [self.bottomMiddleFormView indexForCell:cell];
    }
    return index;
}

- (CGRect)frameOfCellAtIndex:(CFormCellIndex *)index
{
    if (index != nil)
    {
        CFormView *formView = [self formViewForIndex:index];
        CFormCellIndex *realIndex = [self realIndexWithIndex:index];
        CGRect frame = [formView frameOfCellAtIndex:realIndex];
        return [self convertRect:frame fromView:formView];
    }
    return CGRectZero;
}

- (void)scrollToIndex:(CFormCellIndex *)index animated:(BOOL)animated
{
    if (index != nil)
    {
        CFormView *formView = [self formViewForIndex:index];
        CFormCellIndex *realIndex = [self realIndexWithIndex:index];
        [formView scrollToIndex:realIndex animated:animated];
    }
}

- (void)scrollToFormContentOffset:(CGPoint)offset
{
    self.topMiddleFormView.contentOffset = CGPointMake(offset.x, 0);
    self.leftMiddleFormView.contentOffset = CGPointMake(0, offset.y);
    self.middleFormView.contentOffset = offset;
    self.bottomMiddleFormView.contentOffset = CGPointMake(offset.x, 0);
}

- (void)setCurSelectedIndex:(CFormCellIndex *)curSelectedIndex
{
    CFormView *curSelView = [self formViewForIndex:curSelectedIndex];
    if (_curSelectedIndex != nil)
    {
        CFormView *prevSelView = [self formViewForIndex:_curSelectedIndex];
//        CFormView *curSelView = [self formViewForIndex:curSelectedIndex];
        if (prevSelView != curSelView)
        {
            prevSelView.curSelectedIndex = nil;
        }
    }
    curSelView.curSelectedIndex = [self realIndexWithIndex:curSelectedIndex];
    
    CFormCellIndex *tmp = [curSelectedIndex retain];
    [_curSelectedIndex release];
    _curSelectedIndex = tmp;
}

#pragma mark - form view delegate

- (void)updateCellUI:(CFormCellView *)cell withIndex:(CFormCellIndex *)index inView:(CFormView *)view
{
    if (self.style == EFreezeFormStyleClassic)
    {
        if (view == self.leftTopFormView || view == self.topMiddleFormView)
        {
//            cell.highlightable = NO;
            cell.borderMask = EFormBorderRight;
            if (view == self.topMiddleFormView && index.col == view.colNumber-1)
            {
                cell.borderMask = EFormBorderNone;
            }
            cell.borderColor = [UIColor whiteColor];
        //    cell.backgroundColor = self.renderColor;
        }
        else if (view == self.leftMiddleFormView)
        {
//            cell.highlightable = NO;
            cell.borderMask = EFormBorderRight;
            if (self.freezeBottomRowNum > 0 || index.row < view.rowNumber-self.freezeBottomRowNum-1)
            {
                cell.borderMask |= EFormBorderBottom;
            }
            cell.borderColor = [UIColor lightGrayColor];
        //    cell.backgroundColor = [UIColor colorWithWhite:.95 alpha:1.0];
            cell.backgroundColor = [UIColor whiteColor];
        }
        else if (view == self.middleFormView)
        {
//            cell.highlightable = YES;
            cell.borderMask = EFormBorderNone;
            if (self.freezeBottomRowNum > 0 || index.row < view.rowNumber-1)
            {
                cell.borderMask = EFormBorderBottom;
            }
            if (index.col < view.colNumber-1)
            {
                cell.borderMask |= EFormBorderRight;
            }
            cell.borderColor = [UIColor lightGrayColor];
            cell.backgroundColor = [UIColor whiteColor];
        }
        else if (view == self.leftBottomFormView || view == self.bottomMiddleFormView)
        {
//            cell.highlightable = NO;
            cell.borderMask = EFormBorderNone;
            if (index.row < view.rowNumber-1)
            {
                cell.borderMask = EFormBorderBottom;
            }
            if (view == self.leftBottomFormView || index.col < view.colNumber-1)
            {
                cell.borderMask |= EFormBorderRight;
            }
            cell.borderColor = [UIColor lightGrayColor];
            cell.backgroundColor = [UIColor whiteColor];
        }
        
        if (cell.isFocus == YES)
        {
            cell.backgroundColor = [UIColor colorWithHex:0xF1F6CB];
        }
    }
}

- (NSUInteger)numberOfColsWithFormView:(CFormView *)view
{
    if (self.leftTopFormView == view || self.leftMiddleFormView == view || self.leftBottomFormView == view)
    {
        return self.freezeColNum;
    }
    else
    {
        return self.colNum-self.freezeColNum;
    }
}

- (NSUInteger)numberOfRowsWithFormView:(CFormView *)view
{
    if (self.leftTopFormView == view || self.topMiddleFormView == view)
    {
        return self.freezeTopRowNum;
    }
    else if (self.leftBottomFormView == view || self.bottomMiddleFormView == view)
    {
        return self.freezeBottomRowNum;
    }
    else
    {
        if (self.rowNum < self.freezeTopRowNum+self.freezeBottomRowNum)
        {
            return 0;
        }
        return self.rowNum-self.freezeTopRowNum-self.freezeBottomRowNum;
    }
}

- (CFormCellView *)formView:(CFormView *)view cellAtIndex:(CFormCellIndex *)index
{
    CFormCellView *cell = [self.delegate freezeFormView:self cellAtIndex:[self realIndexWithIndex:index inFormView:view]];
    if (self.style != EFreezeFormStyleNone)
    {
        [self updateCellUI:cell withIndex:index inView:view];
    }
    return cell;
}

- (void)formView:(CFormView *)view didClickCellAtIndex:(CFormCellIndex *)index
{
    CFormCellIndex *realIndex = [self realIndexWithIndex:index inFormView:view];
    self.curSelectedIndex = realIndex;
    if ([self.delegate respondsToSelector:@selector(freezeFormView:didClickCellAtIndex:)])
    {
        [self.delegate freezeFormView:self didClickCellAtIndex:realIndex];
    }
}

- (CGFloat)formView:(CFormView *)view heightForRow:(NSUInteger)row
{
    if ([self.delegate respondsToSelector:@selector(freezeFormView:heightForRow:)])
    {
        return [self.delegate freezeFormView:self heightForRow:[self realIndexWithIndex:[CFormCellIndex indexWithtRow:row col:0] inFormView:view].row];
    }
    return FORMVIEW_DEF_ROW_H;
}

- (CGFloat)formView:(CFormView *)view widthForCol:(NSUInteger)col
{
    if ([self.delegate respondsToSelector:@selector(freezeFormView:widthForCol:)])
    {
        return [self.delegate freezeFormView:self widthForCol:[self realIndexWithIndex:[CFormCellIndex indexWithtRow:0 col:col] inFormView:view].col];
    }
    return FORMVIEW_DEF_COL_W;
}

- (CGPoint)formContentOffset
{
    return self.middleFormView.contentOffset;
}

- (CGSize)contentDisplaySize
{
    CGSize displaySize = self.contentView.bounds.size;
    if (self.middleFormView.contentSize.width > self.middleFormView.width)
    {
        if (self.middleFormView.contentOffset.x > self.middleFormView.contentSize.width - self.middleFormView.width)
        {
            displaySize.width = self.middleFormView.contentSize.width - self.middleFormView.contentOffset.x + leftTopSepPoint.x;
        }
        if (displaySize.width < self.leftMiddleFormView.width)
        {
            displaySize.width = self.leftMiddleFormView.width;
        }
    }
    else
    {
        displaySize.width = self.middleFormView.right-(self.middleFormView.width-self.middleFormView.contentSize.width);
    }
    
    if (self.middleFormView.contentSize.height > self.middleFormView.height)
    {
        if (self.middleFormView.contentOffset.y > self.middleFormView.contentSize.height - self.middleFormView.height)
        {
            displaySize.height = self.middleFormView.contentSize.height - self.middleFormView.contentOffset.y + leftTopSepPoint.y+self.bottomMiddleFormView.height;
        }
        if (displaySize.height < self.topMiddleFormView.height+self.bottomMiddleFormView.height)
        {
            displaySize.height = self.topMiddleFormView.height+self.bottomMiddleFormView.height;
        }
    }
    else
    {
        displaySize.height = self.middleFormView.bottom-(self.middleFormView.height-self.middleFormView.contentSize.height)+self.bottomMiddleFormView.height;
    }
    return displaySize;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 0 || scrollView.contentOffset.y < 0)
    {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x < 0 ? 0 : scrollView.contentOffset.x, scrollView.contentOffset.y < 0 ? 0 : scrollView.contentOffset.y);
    }
    if (scrollView == self.topMiddleFormView)
    {
        self.middleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.middleFormView.contentOffset.y);
        self.bottomMiddleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.bottomMiddleFormView.contentOffset.y);
    }
    else if (scrollView == self.bottomMiddleFormView)
    {
        self.middleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.middleFormView.contentOffset.y);
        self.topMiddleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.topMiddleFormView.contentOffset.y);
    }
    else if (scrollView == self.leftMiddleFormView)
    {
        self.middleFormView.contentOffset = CGPointMake(self.middleFormView.contentOffset.x, scrollView.contentOffset.y);
    }
    else if (scrollView == self.middleFormView)
    {
        self.topMiddleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.topMiddleFormView.contentOffset.y);
        self.leftMiddleFormView.contentOffset = CGPointMake(self.leftMiddleFormView.contentOffset.x, scrollView.contentOffset.y);
        self.bottomMiddleFormView.contentOffset = CGPointMake(scrollView.contentOffset.x, self.bottomMiddleFormView.contentOffset.y);
    }
    
    if ([self.delegate respondsToSelector:@selector(freezeFormView:contentOffsetChange:)])
    {
        [self.delegate freezeFormView:self contentOffsetChange:self.middleFormView.contentOffset];
    }
    
    CGSize curDisplaySize = [self contentDisplaySize];
    if (!CGSizeEqualToSize(curDisplaySize, self.curContentDisplaySize))
    {
        if ([self.delegate respondsToSelector:@selector(freezeFormView:contentSizeChange:)])
        {
            [self.delegate freezeFormView:self contentSizeChange:curDisplaySize];
        }
        self.curContentDisplaySize = curDisplaySize;
    }

    if (self.middleFormView.contentOffset.x > 0 && self.freezeColNum > 0 && _leftMiddleSepView.alpha == 0.0)
    {
        _leftMiddleSepView.alpha = 1.0;
    }
    else if (self.middleFormView.contentOffset.x == 0 && self.freezeColNum > 0 && _leftMiddleSepView.alpha == 1.0)
    {
        _leftMiddleSepView.alpha = 0.0;
    }
    
    if (self.middleFormView.contentOffset.y > 0 && self.freezeTopRowNum > 0 && _topMiddleSepView.alpha == 0.0)
    {
        _topMiddleSepView.alpha = 1.0;
    }
    else if (self.middleFormView.contentOffset.y == 0 && self.freezeTopRowNum > 0 && _topMiddleSepView.alpha == 1.0)
    {
        _topMiddleSepView.alpha = 0.0;
    }
    
    if (self.middleFormView.contentOffset.y+self.middleFormView.height < self.middleFormView.contentSize.height && self.freezeBottomRowNum > 0 && _bottomMiddleSepView.alpha == 0.0)
    {
        _bottomMiddleSepView.alpha = 1.0;
    }
    else if (self.middleFormView.contentOffset.y+self.middleFormView.height >= self.middleFormView.contentSize.height && self.freezeBottomRowNum > 0 && _bottomMiddleSepView.alpha == 1.0)
    {
        _bottomMiddleSepView.alpha = 0.0;
    }
}

@end
